/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.view;

import java.util.Iterator;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class ExampleTreeViewer extends ViewPart {

    public static final String VIEW_ID = "org.apache.pig.pigpen.view.ExampleTreeViewer";

    protected Text text;
    protected TreeViewer treeViewer;
    protected ExampleLabelProvider labelProvider;

    // private Map<LogicalOperator, DataBag> derivedData;
    private ExampleData exampleData;

    @Override
    public void createPartControl(Composite parent) {
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.verticalSpacing = 2;
        layout.marginWidth = 0;
        layout.marginHeight = 2;
        parent.setLayout(layout);

        text = new Text(parent, SWT.READ_ONLY | SWT.SINGLE | SWT.BORDER);
        // layout the text field above the treeviewer
        GridData layoutData = new GridData();
        layoutData.grabExcessHorizontalSpace = true;
        layoutData.horizontalAlignment = GridData.FILL;
        text.setLayoutData(layoutData);

        // Create the tree viewer as a child of the composite parent
        treeViewer = new TreeViewer(parent);
        treeViewer.setContentProvider(new ExampleContentProvider());
        labelProvider = new ExampleLabelProvider();
        treeViewer.setLabelProvider(labelProvider);

        treeViewer.setUseHashlookup(true);

        // layout the tree viewer below the text field
        layoutData = new GridData();
        layoutData.grabExcessHorizontalSpace = true;
        layoutData.grabExcessVerticalSpace = true;
        layoutData.horizontalAlignment = GridData.FILL;
        layoutData.verticalAlignment = GridData.FILL;
        treeViewer.getControl().setLayoutData(layoutData);

        hookListerners();

        // treeViewer.setInput(getInitalInput());
        // treeViewer.setInput(exampleData);
        // treeViewer.expandAll();

    }

    public void setInput(ExampleData data) {
        exampleData = data;
        treeViewer.setInput(data);
    }

    private void hookListerners() {
        treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                if (event.getSelection().isEmpty()) {
                    text.setText("");
                    return;
                }
                if (event.getSelection() instanceof IStructuredSelection) {
                    IStructuredSelection selection = (IStructuredSelection) event
                            .getSelection();
                    StringBuffer toShow = new StringBuffer();
                    for (Iterator iterator = selection.iterator(); iterator
                            .hasNext();) {
                        IDataContainer domain = (IDataContainer) iterator
                                .next();
                        // String value = labelProvider.getText(domain);
                        String value = domain.getShortName();
                        toShow.append(domain.getType());
                        toShow.append(" : ");
                        toShow.append(value);
                        toShow.append(", ");
                    }
                    // remove the trailing comma space pair
                    if (toShow.length() > 0) {
                        toShow.setLength(toShow.length() - 2);
                    }
                    text.setText(toShow.toString());
                }
            }
        });
    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub
        treeViewer.getTree().setFocus();
        text.setFocus();
    }

}
